package com.anolesoft.amljmes.foreknow.controller;

import com.anolesoft.amljmes.foreknow.bo.MesBzzQfbld;
import com.anolesoft.amljmes.foreknow.bo.MesBzzQfbldmx;
import com.anolesoft.amljmes.foreknow.dto.MesBzzPtdDto;
import com.anolesoft.amljmes.foreknow.dto.MesBzzQfbldDto;
import com.anolesoft.amljmes.foreknow.service.MesBzzPtdmxService;
import com.anolesoft.amljmes.foreknow.service.MesBzzQfbldService;
import com.anolesoft.amljmes.foreknow.service.STFEmployeeService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mesbzzqfbld")
public class MesBzzQfbldController {

    @Autowired
    private MesBzzQfbldService service;

    @Autowired
    private MesBzzPtdmxService ptdmxService;

    @Autowired
    private STFEmployeeService employeeService;

    /**
     * 添加到铅封保留单及明细
     * @param dto
     * @return
     */
    @RequestMapping(value = "/insertqfbld" , method = RequestMethod.POST)
    public AjaxResult queryMesBzzQfbld(@RequestBody MesBzzPtdDto dto){
        int a = service.insertData(dto.getList());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
    }

    /**
     * 查询铅封保留单
     * @param dto
     * @return
     */
    @RequestMapping(value = "/query" , method = RequestMethod.POST)
        public AjaxResult queryQfbld(@RequestBody MesBzzQfbldDto dto){
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        dto.setFactoryCode(loginUser.getFacCode());
        List<MesBzzQfbld> allList = service.query(dto);
        int endIndex = pageNum * pageSize > allList.size() ? allList.size() : pageNum * pageSize;
        List<MesBzzQfbld> list = allList.subList( (pageNum-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(list, pageSize, pageNum, allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
//        List<MesBzzQfbld> listAll = new ArrayList<>();
//        for (MesBzzQfbld mesBzzQfbld:allList) {
//            if (mesBzzQfbld.getCjr() != null){
//                String name =  employeeService.getname(mesBzzQfbld.getCjr());
//                mesBzzQfbld.setCjr(name);
//                listAll.add(mesBzzQfbld);
//            }
//        }
//        int endIndex = pageNum * pageSize > listAll.size() ? allList.size() : pageNum * pageSize;
//        List<MesBzzQfbld> list = listAll.subList( (pageNum-1) *pageSize, endIndex);
//        PageResult pageResult = new PageResult(list, pageSize, pageNum, listAll.size());
//        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    /**
     * 查询铅封保留单明细
     * @param id
     * @return
     */
    @RequestMapping(value = "/querymx" , method = RequestMethod.POST)
    public AjaxResult queryQfbldmx(@RequestBody String id){
//        System.out.println(id);
        String idd = id.substring(0,id.length()-1);
        List<MesBzzQfbldmx> allList = service.queryMx(idd);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,allList);
    }

    /**
     * 修改铅封保留单明细状态(撤回按钮)
     * @param
     * @return
     */
    @Transactional
    @RequestMapping(value = "/updatemxstate" , method = RequestMethod.POST)
    public AjaxResult updateQfbldmx(@RequestBody MesBzzQfbldmx mesBzzQfbldmx){
        //改变铅封保留单明细状态(判断a)
        int a = service.updatemxState(mesBzzQfbldmx.getObjuid());
        //改变配套单保留数量
        int b = service.updateNum(mesBzzQfbldmx.getPtdmxUid());
        //改变铅封保留单明细保留数量
//        int f = service.updateqfbldnum(mesBzzQfbldmx.getObjuid());
//        int f = service.updateqfbldnum(mesBzzQfbldmx.getPtdmxUid());
        //改变配套单状态
        int e = service.updateptdstate(mesBzzQfbldmx.getPtdmxUid());
        //查询铅封保留单下是否还存在明细
        int c = service.selectNum(mesBzzQfbldmx.getQfbldUid());
//        System.out.println(c);
        if (c == 0){
            //不存在明细改变铅封保留单状态
            int d = service.updatebldstate(mesBzzQfbldmx.getQfbldUid());
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
    }

    /**
     * 打印
     * @param id
     * @return
     */
    @RequestMapping(value = "/querydmx" , method = RequestMethod.POST)
    public AjaxResult querydmx(@RequestBody String id){
        String idd = id.substring(0,id.length()-1);
        List<MesBzzQfbldmx> allList = service.querydmingxi(idd);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,allList);
    }
}
